<!--
 * @Author: 李小晴 2689279066@qq.com
 * @Date: 2023-08-10 17:29:24
 * @LastEditors: 李小晴 2689279066@qq.com
 * @LastEditTime: 2024-01-02 15:21:00
 * @FilePath: \sys\src\views\SmartPasture\Raise\Feed\components\Assets.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="ts">
import { reactive } from 'vue'
import { Table } from '@/components/Table'
import { spFeedAssetListApi } from '@/api/sp/feed'
import { useTable } from '@/hooks/web/useTableTest'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useCache } from '@/hooks/web/useCache'

const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const props = defineProps({
  id: {
    type: String,
    default: null
  }
})
const spFeedAssetList = async (val) => {
  val.feed_id = props.id
  return spFeedAssetListApi(val)
}
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: spFeedAssetList,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList } = methods

getList()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 30
  },
  {
    field: 'asset_code',
    label: '注射编号',
    width: 200
  },
  {
    field: 'asset_code_2',
    label: '项圈编号',
    width: 200
  },
  {
    field: 'earcon_code',
    width: 200,
    label: '耳标编号'
  },
  {
    field: 'breed',
    label: '畜别',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed', cellValue)
    },
    width: 100
  },
  {
    field: 'breed_sub',
    width: 200,
    label: '品种',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed_sub', cellValue)
    }
  }
])
const { allSchemas } = useCrudSchemas(crudSchemas)
</script>

<template>
  <Table
    v-model:pageSize="tableObject.pageSize"
    v-model:currentPage="tableObject.currentPage"
    :columns="allSchemas.tableColumns"
    :data="tableObject.tableList"
    :loading="tableObject.loading"
    :selection="false"
    :border="true"
    :pagination="{
      total: tableObject.count
    }"
    @register="register"
  />
</template>
